package top.xana.acg.anime.network.response

import androidx.annotation.IntDef
import top.xana.acg.anime.utils.GsonUtils
import java.io.Serializable

/**
 * @author xana
 * @date 2022/4/12 0012 19:33
 * @email cv4096@qq.com
 **/

open class Response<T>(
    @StatueCode val code: Int = StatueCode.CODE_OK
) : Serializable {
    companion object {
        const val TAG = "Response"
    }

    private val msg: String? = null

    private val message: String = ""

    private val `data`: T? = null

    private val result: T? = null

    fun ok() = code == StatueCode.CODE_OK

    fun msg() = msg ?: message

    open fun result() = `data` ?: result

    override fun toString() = GsonUtils.toJson(this) ?: ""
}

@IntDef(StatueCode.CODE_OK, StatueCode.CODE_NOT_FOUND,StatueCode.CODE_FORBIDDEN, StatueCode.CODE_SERVER_ERROR)
@Target(AnnotationTarget.FIELD, AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.LOCAL_VARIABLE)
annotation class StatueCode {
    companion object {
        const val CODE_OK = 200
        const val CODE_FORBIDDEN = 403
        const val CODE_NOT_FOUND = 404
        const val CODE_SERVER_ERROR = 500
    }
}